package com.yc.game.controller;

import com.github.pagehelper.PageInfo;
import com.yc.game.pojo.Category;
import com.yc.game.pojo.Food;
import com.yc.game.service.CategoryService;
import com.yc.game.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class FoodController {
    @Autowired
    private FoodService foodService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询商品
     * @param modelMap
     * @param pageSize
     * @param PageNum
     * @return
     */
    @RequestMapping("page/food")
    public String shopPage(ModelMap modelMap, @RequestParam("categoryId") Integer categoryId,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer PageNum) {
        PageInfo<Food> pageInfo = foodService.getAll(pageSize, PageNum, categoryId);
        modelMap.addAttribute("pageInfo", pageInfo);
        List<Category> categories = categoryService.getAll();
        modelMap.addAttribute("categories", categories);
        modelMap.addAttribute("categoryId", categoryId);
        return "food";
    }


    /**
     * 根据名称模糊查询商品
     * @param modelMap
     * @param pageSize
     * @param PageNum
     * @return
     */
    @RequestMapping("page/findFood")
    public String shopPage(ModelMap modelMap, @RequestParam("foodName") String  foodName,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer PageNum) {
        PageInfo<Food> pageInfo = foodService.findFood(pageSize, PageNum, foodName);
        modelMap.addAttribute("pageInfo", pageInfo);
        List<Category> categories = categoryService.getAll();
        modelMap.addAttribute("categories", categories);
        modelMap.addAttribute("foodName", foodName);
        return "foodSelect";
    }


    /**
     * 根据id查询商品明细
     * @return
     */
    @RequestMapping("page/foodDetail")
    public String productDetail(@RequestParam("foodId") Integer foodId, ModelMap modelMap) {
        Food food = foodService.getById(foodId);
        if (food == null) {
            return "404";
        }
        List<Category> categories = categoryService.getAll();
        modelMap.addAttribute("categories", categories);
        modelMap.addAttribute("food", food);
        return "foodDetail";
    }


}
